package com.bjpowernode.controller;

import com.bjpowernode.dao.StudentDao;
import com.bjpowernode.entity.Student;
import com.bjpowernode.utils.MybatisUtil;
import com.fasterxml.jackson.databind.ObjectMapper;
import org.apache.ibatis.session.SqlSession;

import javax.servlet.*;
import javax.servlet.http.*;
import javax.servlet.annotation.*;
import java.io.IOException;
import java.io.Writer;
import java.util.List;

@WebServlet("/StudentCountServlet")
public class StudentCountServlet extends HttpServlet {
    @Override
    protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        SqlSession sqlSession= MybatisUtil.getSqlSession();
        StudentDao dao=sqlSession.getMapper(StudentDao.class);
        Integer studentCount=dao.selectStudentCount();
        int onePageStudentNum=3;
        int pageNum=1;
        if(studentCount%onePageStudentNum==0){
            pageNum=studentCount/onePageStudentNum;
        }else{
            pageNum=(int)studentCount/onePageStudentNum+1;
        }
        sqlSession.close();
        ObjectMapper om=new ObjectMapper();
        String json=om.writeValueAsString(pageNum);
        Writer out=response.getWriter();
        out.write(json);
    }

    @Override
    protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {

    }
}
